# -*- coding: utf-8 -*-

#    Copyright (C) 2014 by Promotux
#                       di Francesco Meloni snc - http://www.promotux.it/

#    Author: Francesco Meloni  <francesco@promotux.it>

#    This file is part of OdCollect.

#    OdCollect is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 2 of the License, or
#    (at your option) any later version.

#    OdCollect is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.

#    You should have received a copy of the GNU General Public License
#    along with OdCollect.  If not, see <http://www.gnu.org/licenses/>.



from web_lib.page import Page
from web_lib.webutils import Response, expose
from web_lib.webutils import leggiInSessione , permalinkaTitle

@expose('/italiaCives/cms')
def cms_ic(req, subdomain=None):
    """
    Home page
    """
    if not leggiInSessione(sezione="Main", chiave="schema"):
        url_red = "/italiaCives"
    tipo = req.args.get("tipo")
    show = req.args.get("show")
    from dao.StaticPages import StaticPages
    from dao.StaticPagesCategory import StaticPagesCategory
    tipoo = StaticPagesCategory().select(denominazione_breve=tipo)
    if tipoo:
        idtipo=tipoo[0].id
    else:
        idtipo=None
    statics = StaticPages().select(id_categoria=idtipo, batchSize=None)
    pageData = {'file' : "ic/cms",
                "statics": statics,
                "show":show,
                    }
    return Page(req).render(pageData)

@expose('/italiaCives/main_cms/<pages>')
def main_staticpages_detail_ic(req,pages=None, subdomain=None):
    """ Funzione per la visualizzazione pagine statiche
    """
    from dao.Main_StaticPages import MainStaticPages
    staticPages = MainStaticPages(req=req).select(permalink=pages)
    if not staticPages:
        staticPages = MainStaticPages(req=req).select(titlePage=pages)
        if staticPages:
            staticPages[0].permalink = permalinkaTitle(pages)
            staticPages[0].persist()
    if staticPages:
        staticPages = staticPages[0]
    #try:
        #staticPages.clicks = staticPages.clicks +1
    #except:
        #staticPages.clicks = 1
    pageData = {'file' : '/pg/preview',
                'news' : staticPages}
                #"bodyTags":["staticPages"]}
    staticPages.persist()
    return Page(req).render(pageData)

@expose('/italiaCives/cms/<pages>')
def staticpages_detail_ic(req,pages=None, subdomain=None):
    """ Funzione per la visualizzazione pagine statiche
    """
    from dao.StaticPages import StaticPages
    staticPages = StaticPages(req=req).select(permalink=pages)
    if not staticPages:
        staticPages = StaticPages(req=req).select(titlePage=pages)
        if staticPages:
            staticPages[0].permalink = permalinkaTitle(pages)
            staticPages[0].persist()
    if staticPages:
        staticPages = staticPages[0]
    try:
        staticPages.clicks = staticPages.clicks +1
    except:
        staticPages.clicks = 1
    pageData = {'file' : '/pg/preview',
                'news' : staticPages}
                #"bodyTags":["staticPages"]}
    staticPages.persist()
    return Page(req).render(pageData)
